function DropList(response,code,objid){
	var dl = $("#droplist");
    var obj = $("#"+objid);
    if (! obj.length) {
        console.error('Object #'+objid + ' Not Found.');
        return;
    }
    
	var c = $("#content");
	if(!code){
		var pos = obj.position();
		var height = obj.outerHeight();
		obj[0].parentNode.insertBefore(dl[0],obj[0]);
		var scrollTop = parseInt(c[0].scrollTop);
        
        dl.css({
            left:  pos.left,
            top:   pos.top+height+scrollTop+5,
            width: obj.width() + 7
        });
		//dl[0].style.left = (pos.left)+"px";
		//dl[0].style.top = (pos.top+height+scrollTop+5)+"px";
		dl.html(response);
		dl.show();
	}else{
		dl.html("");
		DropListOff();
	}
}
function DropListOff(){
	var dl = $("#droplist");
	dl.html("");
	dl.appendTo("body");
	dl.hide();
}

function DropListBrands(inpt,name){
	inpt.value=inpt.value.replace(/^[\s\r\n\t]+|[\s\r\n\t]+$/,"")
	if(inpt.value.length>1){
		Request('/brands/search/'+escape(inpt.value)+"/"+name,'DropList(\''+name+'\')');
	}else{
		DropListOff()
	}
}
function SelectBrand(btn){
	var id = parseInt(btn.getAttribute("value"));
	var name = btn.innerHTML;
	var obj = $("#"+btn.getAttribute("object"));
	obj[0].value = name;
	obj[0].focus();
	obj[0].select();
	DropListOff();
}
function DropListModel(inpt,brandname,modelname){
	if(inpt.value.length>1){
		brandval = $("#"+brandname);
        brandval = brandval[0].value; brandval=brandval.length>0?brandval:'n';
		Request('/models/search/'+escape(inpt.value)+"/"+brandval+"/"+brandname+"/"+modelname,'DropList(\''+modelname+'\')');
	}else{
		DropListOff()
	}
}
function SelectModel(btn){
	var id = parseInt(btn.getAttribute("value"));
	var name = btn.innerHTML;
	
	var data = btn.getAttribute("data").split("{!}");
	var brand = data[0];
	var model = data[1];
	
	var objects = btn.getAttribute("object").split(";",2);
	var obj = $("#"+objects[0]);
	obj[0].value = brand;
	obj = $("#"+objects[1]);
	obj[0].value = model;
	
	obj[0].focus();
	obj[0].select();
	DropListOff();
}
function Request(url,responseFunc){
	responseFunc = responseFunc.length>0?responseFunc:"AfterRequest";
	var opts = ({
		type: "GET",
		url: url,
		complete: function(request){
			document.body.evalCode = request.responseText
			var code = request.getResponseHeader("Status");
			code = code>0?code:0;
			if(responseFunc.match(/\(/)){
				var openZ = responseFunc.indexOf("(");
				var closeZ = responseFunc.lastIndexOf(")");
				var fname = responseFunc.substr(0,openZ);
				var attrs = responseFunc.substr(openZ+1,closeZ-openZ-1);
				eval(fname+"(document.body.evalCode,"+code+","+attrs+")");
			}else{
				eval(responseFunc+"(document.body.evalCode,"+code+")");
			}
		}
	});
	$.ajax(opts);
}